#!/bin/bash -e

# snp for parallel using segmentation

function info() {
Usage: `basename $0` '[-lnsgGacer] reads1.fq.gz reads2.fq.gz'
exit 65
}

while getopts  ":l:p:f:n:s:gGa:ce:r:" opts; do
	case  $opts  in
	a) cut=$OPTARG;;
	c) cnv=T;;
	l) interval=$OPTARG;;
	n) nums=$OPTARG;;
	s) sample_name=$OPTARG;;
	g) single_cell=T;;
	G) somatic=T;;
	p) out_prefix=$OPTARG;;
	f) suffix=$OPTARG;;
	e) num_pre=$OPTARG;;
	r) fq_rows=$OPTARG;;
	*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 2 ]; then info; fi


script=$(mktemp $(basename $0).$(date +%m%d%H%M).XXXXXX)


r1=`rel_abs.sh $1`
r2=`rel_abs.sh $2` # must run before mkdir=true
r12=`echo $r1 $r2`

makedir=true

if test -z "$interval"; then interval=$data_path/intervals/1/b37.bed; fi
if test -z "$num_pre"; then num_pre=400; fi
if test -z "$fq_rows"; then fq_rows=4000000; fi

. $var

echo mnt=monitor.sh
echo 
echo fastqstat=fastqstat.sh
echo fastqc=fastqc.sh
echo summ=summ.sh
echo 
echo split_fastq=split_fastq.sh
echo 
echo if test -z "$cut"; then
echo cutadp=cutadp.sh
echo elif test "$cut" = "general"; then
echo cutadp=cutadp.sh
echo elif test "$cut" = "malbac"; then
echo cutadp=cutadp_malbac.sh
echo elif test "$cut" = "ctc80"; then
echo cutadp=cutadp_ctc80_discard_trimmed.sh
echo fi
echo 
echo aln=aln_bwa_bt.sh
echo sam2b=sam2b_samtools.sh
echo merge=merge_bam.sh
echo hmmcopy=hmmcopyb38.sh
echo split_bam=split_bam_0701.sh
echo bed_split=bed-split.py
echo extract_bam=extract_bam_bed.sh
echo extract_bam_bed=extract_bam_bed.sh
echo markdup=deldup.sh
echo realn=realign_bt.sh
echo recal=recal0630.sh
echo ug=ug.sh
echo annos=annos0711.sh
echo combine_vcfs=combine_vcfs.sh
echo impact=impact.sh
echo format_simple=format_simple_0629.sh
echo 
echo fq_rename=snpp_fq_rename.sh
echo 
echo lns_file=lns_file.sh
echo bk=bk.sh
echo end=end.sh
echo 
echo cpu2=2
echo cpu4=4
echo cpu8=8
echo mem4=4
echo mem8=8
echo mem16=16
echo mem32=32
echo 
echo 
echo fq_list=$out_prefix.fq.list.txt
echo cutadp_list=$out_prefix.cutadp.list.txt
echo sam_list=$out_prefix.sam.list.txt
echo bam_list=$out_prefix.bam.list.txt
echo bed_list=$out_prefix.bed.list.txt
echo merge_list=$out_prefix.merge.list.txt
echo split_list=$out_prefix.split.list.txt
echo markdup_list=$out_prefix.markdup.list.txt
echo 
echo i_bed_list=$out_prefix.i.bed.list.txt
echo l_bed_list=$out_prefix.l.bed.list.txt
echo realn_list=$out_prefix.realn.list.txt
echo recal_list=$out_prefix.recal.list.txt
echo ug_list=$out_prefix.ug.list.txt
echo high_list=$out_prefix.high.list.txt
echo low_list=$out_prefix.low.list.txt
echo hl_list=$out_prefix.hl.list.txt
echo combine_list=$out_prefix.combine.list.txt
echo impact_high_list=$out_prefix.impact.high.list.txt
echo impact_low_list=$out_prefix.impact.low.list.txt
echo impact_hl_list=$out_prefix.impact.hl.list.txt
echo end_list=$out_prefix.end.list.txt
echo 
echo snapshot=$out_prefix.snapshot.txt
echo test -e $snapshot || echo -e "Sample: $sample_name\nRaw data: $r12\nPanel: $interval\nResults: ${http}/$desti/" >$snapshot
echo 
echo 
echo echo start_id=$(sub -F -nmnt -m1 -p1 $mnt `basename $0` $@)
echo 
echo echo # fq_stat
echo echo fqstat_id=$(sub -F -nstat -m8 -p2 $fastqstat -s$sample_name -t2 -p$out_prefix $r12)
echo echo fqc_id=$(sub -F -nfqc -m8 -p2 $fastqc -t2 -s$sample_name $r12)
echo 
echo echo # end fq_stat
echo 
echo split1_id=$(sub -F -nsplt1 -m2 -p2 "pigz -cdf $r1|split_fastq.sh -l$fq_rows -p$out_prefix.1.split -")
echo 
echo split2_id=$(sub -F -nsplt2 -m2 -p2 "pigz -cdf $r2|split_fastq.sh -l$fq_rows -p$out_prefix.2.split -")
echo 
echo 
echo fq_rename_id=$(sub -d"afterok:$split1_id:$split2_id" -F -nrenam -m1 -p1 $fq_rename)
echo 
echo 
echo for i in $(seq $num_pre); do
echo 
echo cutadp_id[$i]=$(sub -d"afterok:$fq_rename_id" -F -nadp.$i -m2 -p1 $cutadp -p$out_prefix.$i $out_prefix.1.split.$i.fastq $out_prefix.2.split.$i.fastq)
echo 
echo aln_id[$i]=$(sub -d"afterok:${cutadp_id[$i]}" -F -naln.$i -m6 -p4 $aln -p$out_prefix.$i -s$sample_name -t4 $out_prefix.$i.1.cut.fastq $out_prefix.$i.2.cut.fastq)
echo 
echo sam2b_id[$i]=$(sub -d"afterok:${aln_id[$i]}" -F -n2b.$i -m4 -p2 $sam2b -p$out_prefix.$i -t2 $out_prefix.$i.sam)
echo 
echo sam2b_ids=$sam2b_ids:${sam2b_id[$i]}
echo done
echo 
echo merge_id=$(sub -d"afterany$sam2b_ids" -F -nmerg -m16 -p6 $merge -t6 $out_prefix.*.sort.bam) # if there is only one bam file, it goes wrong.
echo 
echo if test -z "$nums"; then nums=120; fi
echo 
echo cat $interval|cut -f1-3 >$out_prefix.bed
echo 
echo $bed_split $out_prefix.bed $nums
echo 
echo for i in $(seq $nums); do
echo 
echo extra_id[$i]=$(sub -d"afterok:$merge_id" -F -nextra.$i -m4 -p2 $extract_bam -p$out_prefix.$i $out_prefix.merge.bam $out_prefix.$i.bed)
echo 
echo mark_id[$i]=$(sub -d"afterok:${extra_id[$i]}" -F -nmark.$i -m4 -p1 $markdup -m -p$out_prefix.$i  $out_prefix.$i.extract.bam)
echo 
echo if test -n "$cnv"; then
echo del_id[$i]=$(sub -d"afterok:${mark_id[$i]}" -F -ndel.$i -m4 -p1 $markdup -p$out_prefix.$i  $out_prefix.$i.markdup.bam)
echo del_ids=$del_ids:${del_id[$i]}
echo fi
echo 
echo realn_id[$i]=$(sub -d"afterok:${mark_id[$i]}" -F -nrealn.$i -m8 -p4 $realn -i$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.markdup.bam)
echo 
echo recal_id[$i]=$(sub -d"afterok:${realn_id[$i]}" -F -nrecal.$i -m8 -p4 $recal -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.realn.bam)
echo 
echo ug_id[$i]=$(sub -d"afterok:${recal_id[$i]}" -F -nug.$i -m12 -p6 $ug -i$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.realn.recal.bam)
echo 
echo 
echo 
echo if test -n "$somatic"; then
echo 	echo; echo somatic
echo 	if test -n "$single_cell"; then
echo 		echo single_cell
echo 		# monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -gm" $ug_list $ug $recal_list &
echo 		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -gm -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
echo 	else
echo 		# monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -m" $ug_list $ug $recal_list &
echo 		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -m -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
echo 	fi
echo else
echo 	echo; echo germline
echo 	if test -n "$single_cell"; then
echo 		echo single_cell
echo 		# monitors.sh -nano -m12 -c6 -i$l_bed_list "$annos -g" $ug_list $ug $recal_list &
echo 		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -g -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
echo 	else
echo 		# monitors.sh -nano -m12 -c6 -i$l_bed_list $annos $ug_list $ug $recal_list &
echo 		ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
echo 	fi
echo fi
echo 
echo 
echo # ano_id[$i]=$(sub -d"afterok:${ug_id[$i]}" -F -nano.$i -m12 -p6 $annos -l$out_prefix.$i.bed -p$out_prefix.$i $out_prefix.$i.snp.vcf $out_prefix.$i.indel.vcf )
echo 
echo echo $out_prefix.$i.high.vcf >> $high_list
echo echo $out_prefix.$i.low.vcf  >> $low_list
echo recal_ids=$recal_ids:${recal_id[$i]}
echo ano_ids=$ano_ids:${ano_id[$i]}
echo done
echo 
echo # summ #note that branch should be monitor
echo merge2_id=$(sub -d"afterany$recal_ids" -F -nmerg2 -m16 -p6 $merge -t6 -p$out_prefix.2 $out_prefix.*.realn.recal.bam)
echo 
echo fqc2_id=$(sub -d"afterok:$merge2_id" -F -nfqc2 -m8 -p2 $fastqc -t2 -s$sample_name $out_prefix.2.merge.bam)
echo 
echo summ_id=$(sub -d"afterok:$merge2_id" -F -nsum -m8 -p2 $summ -g -s$sample_name $out_prefix.2.merge.bam $out_prefix.bed)
echo # end summ
echo 
echo # hmmcopy #note that branch should be monitor
echo if test -n "$cnv"; then
echo merge3_id=$(sub -d"afterany$del_ids" -F -nmerg3 -m16 -p6 $merge -t6 -p$out_prefix.3 $out_prefix.*.deldup.bam)
echo 
echo hmmcopy_id=$(sub -d"afterok:$merge3_id" -F -nhmm -m4 -p1 $hmmcopy $out_prefix.3.merge.bam)
echo lorenz_id=$(sub -d"afterok:$merge3_id" -F -hlrz -m2 -p1 $lorenz $out_prefix.3.merge.bam)
echo 
echo fi
echo 
echo combh_id=$(sub -d"afterok$ano_ids" -F -ncombh -m4 -p2 $combine_vcfs -p$out_prefix.high $high_list)
echo 
echo combl_id=$(sub -d"afterok$ano_ids" -F -ncombl -m4 -p2 $combine_vcfs -p$out_prefix.low $low_list)
echo 
echo 
echo imph_id=$(sub -d"afterok:$combh_id" -F -nimph -m4 -p2 $impact $out_prefix.high.combine.vcf)
echo 
echo impl_id=$(sub -d"afterok:$combl_id" -F -nimpl -m4 -p2 $impact $out_prefix.low.combine.vcf)
echo 
echo 
echo fmth_id=$(sub -d"afterok:$imph_id" -F -nfmth -m10 -p1 $format_simple -p$out_prefix.high $out_prefix.high.combine.af.hg19_multianno.vcf)
echo 
echo fmtl_id=$(sub -d"afterok:$impl_id" -F -nfmtl -m10 -p1 $format_simple -p$out_prefix.low $out_prefix.low.combine.af.hg19_multianno.vcf)
echo 
echo lns_id=$(sub -d"afterok:$fmth_id:$fmtl_id" -F -nlns -m1 -p1 $lns_file -p$sample_name *.pdf *.xls $out_prefix.high.anno.info.txt.zip $out_prefix.low.anno.info.txt.zip $out_prefix.summary.txt $out_prefix.interval_summ.txt $out_prefix.low.combine.impact.drugs-prediction.html $out_prefix.high.combine.impact.drugs-prediction.html)
echo 
echo bk_id=$(sub -d"afterok:$lns_id" -F -nbk -m1 -p1 $bk -tsnp/xxx $sample_name.*)
echo 
echo end_id=$(sub -d"afterok:$fmth_id:$fmtl_id:$lns_id:$bk_id" -F -nen -m1 -p1 $end `basename $0` $@)
echo 
echo . $cmd_done
echo
